K-way Merging of Runs using ‘C’
K-way consolidating, otherwise called k-way union or k-way combine sort, is a method used to blend k arranged records or exhibits into a solitary arranged list. It broadens the idea of combining two arranged records in consolidate sort to deal with multiple rundowns.
This is an outline of the way k-way consolidating works:
1. Instatement:
· Given k arranged records or exhibits.
· Make an unfilled outcome rundown or exhibit to store the blended result.
2. Blend Interaction:
· Introduce k pointers, one for each info list, highlighting the principal component of each rundown.
· Think about the components pointed by the k pointers and select the littlest (or biggest) component.
· Attach the chose component to the outcome rundown or exhibit.
· Increase the pointer of the rundown from which the chose component was taken.
· Rehash the above strides until all components from all info records are converged into the outcome rundown or cluster.
3. Conclusion:
· When all components are consolidated, the outcome rundown or exhibit will be an arranged assortment of components from every one of the information records.
K-way blending is usually executed utilizing a min-store (or max-pile) information structure. At first, the primary component from each rundown is embedded into the min-store. The min-load monitors the base component among every one of the ongoing components pointed by the k pointers. The base component is then removed from the pile and added to the outcome rundown or cluster. The comparing pointer is increased, and the following component from the individual rundown is embedded into the load. This cycle go on until all components are blended.
The time intricacy of k-way blending is O(n log k), where n is the complete number of components across undeniably input records and k is the quantity of info records. It is proficient for combining a lot of information or while managing outer capacity where not all information can be stacked into memory without a moment's delay.
K-way combining tracks down applications in different regions, like outside arranging, data set activities, consolidate join calculations, and blending numerous arranged streams.
Generally speaking, k-way combining is a strategy to blend k arranged records or clusters into a solitary arranged list effectively, utilizing a min-pile (or max-store) information structure. It is a speculation of the union cycle in consolidate sort and is generally utilized in different situations that include blending numerous arranged assortments.
Program:-
#include <stdio.h>
#include <stdio.h>
int main()
{
int
n1,n2,n3,i,j;
int
a[10000], b[10000], c[20000];
printf("Enter the size of first array: ");
scanf("%d",&n1);
printf("Enter the array elements: ");
for(i = 0; i
< n1; i++)
scanf("%d", &a[i]);
a[i]=i;
printf("Enter the size of second array: ");
scanf("%d",&n2);
printf("Enter the array elements: ");
for(i = 0; i
< n2; i++)
scanf("%d", &b[i]);
b[i]=i;
n3 = n1 +
n2;
for(i = 0; i
< n1; i++)
c[i] =
a[i];
for(i = 0; i
< n2; i++)
c[i +
n1] = b[i];
printf("The merged array: ");
for(i = 0; i
< n3; i++)
printf("%d ", c[i]);
printf("\nFinal array after sorting: ");
for(i = 0; i
< n3; i++){
int
temp;
for(j =
i + 1; j < n3; j++) {
if(c[i] > c[j]) {
temp = c[i];
c[i] = c[j];
c[j] = temp;
}
}
}
for(i = 0; i
< n3 ; i++)
printf(" %d ",c[i]);
return 0;
}
Output:-
0 Comments